package LeetCode.month04.day100;

import java.util.Stack;

/**
 * @author 16778
 * @date 2025/11/10 8:11
 * @description
 */
public class No3542 {

    static void main() {
        System.out.println(minOperations(new int[]{0, 2}));
    }

    public static int minOperations(int[] nums) {
        int step = 0;
        int n = nums.length;
        Stack<Integer> stack = new Stack<>();
        stack.push(-1);
        for (int i = 0; i < n; i++) {
            if(nums[i]==0){
                step+=stack.size()-1;
                while(stack.peek()!=-1){
                    stack.pop();
                }
                continue;
            }
             while(nums[i]<stack.peek()){
                 step++;
                 stack.pop();
             }
             if(stack.peek()!=nums[i]) {
                 stack.push(nums[i]);
             }
        }
        while(stack.peek()!=-1){
            step++;
            stack.pop();
        }
        return step;
    }
}
